package homework;

public class FormatCurrency {


    public String formatCurrency(double input){
        StringBuffer stringBuffer = new StringBuffer(input + "");
        stringBuffer.reverse();
        String num = String.valueOf(stringBuffer);

        String[] nums = num.split("\\.");
        int index = nums[1].length() / 3;

        stringBuffer = new StringBuffer(nums[1]);
        int add = 3;
        for (int i = 0; i < index; i++){

            stringBuffer.insert(add, ",");
            add = add + 4;
        }
        nums[1] = String.valueOf(stringBuffer);
        stringBuffer = new StringBuffer(nums[0] + "." + nums[1]);
        stringBuffer.reverse();
        num = String.valueOf(stringBuffer);
        if (num.startsWith(",")){
            num = num.substring(1);
        }

        return num;

    }

}
